using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;
using Nihonaid.classes;
using Nihonaid.Properties;

namespace Nihonaid
{
    /// <summary>
    /// The main application class
    /// </summary>
    static class Program
    {
        #region Properties
        /// <summary>
        /// The learning queue used throughout the application
        /// </summary>
        public static LearningQueue queue;
        /// <summary>
        /// The current item present to the user
        /// </summary>
        public static LearningItem current_item;
        /// <summary>
        /// The form where the learning takes place
        /// </summary>
        public static LearningForm learning_form = null;
        /// <summary>
        /// The form used to monitor the user's progress
        /// </summary>
        public static UserProgressForm progress_form = null;
        /// <summary>
        /// The form used to show details about the application
        /// </summary>
        public static AboutForm about_form = null;
        /// <summary>
        /// The form shown at the beginning of the application
        /// </summary>
        public static SplashForm splash_form = null;
        /// <summary>
        /// The form shown when the application wants to change the settings
        /// </summary>
        public static SettingsForm settings_form = null;
        public static QueueStatusForm queue_status_form = null;
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            // Initialize the forms
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Program.progress_form = new UserProgressForm();
            Program.about_form = new AboutForm();
            Program.settings_form = new SettingsForm();
            Program.queue_status_form = new QueueStatusForm();
            Program.learning_form = new LearningForm();
            Program.splash_form = new SplashForm();
            Program.queue = new LearningQueue();
            Program.current_item = null;

            Program.LoadSettings();
            Program.ShowSplashScreen();

            Application.Run(Program.learning_form);
        }

        /// <summary>
        /// Shows the application's splash screen
        /// </summary>
        public static void ShowSplashScreen()
        {
            // Show the splash screen
            Program.splash_form.Show();
            Thread.Sleep(1000);
            for (double x = 1.0; x >= (double)0.1; x -= 0.1)
            {
                Program.splash_form.Opacity = x;
                Thread.Sleep(10);
            }
            Program.splash_form.Hide();
        }

        /// <summary>
        /// Loads the application's settings 
        /// </summary>
        public static void LoadSettings()
        {
            Configuration.Load();

            queue.SetSchedulerFromSettings();

            // If the last session wasn't today then reset the values for a new session
            if (Configuration.last_session_date.Date < DateTime.Now.Date)
            {
                // Sets the reviewable item limit for today and marks today's session date
                Configuration.todays_items = Configuration.item_treshold;
                Configuration.last_session_date = DateTime.Now;
                Configuration.Save();
            }
        }

        /// <summary>
        /// Closes the application
        /// </summary>
        public static void Dispose()
        {
            Configuration.Save();
            Program.about_form.Hide();
            Program.splash_form.Hide();
            Program.progress_form.Hide();
            Program.learning_form.Hide();
            Program.settings_form.Hide();
            Program.queue_status_form.Hide();
            Program.progress_form.Dispose();
            Program.learning_form.Dispose();
            Program.about_form.Dispose();
            Program.splash_form.Dispose();
            Program.settings_form.Dispose();
            Program.queue_status_form.Dispose();
        }
    }
}
